<?php
/**
 * Created by PhpStorm.
 * User: Lenovo
 * Date: 2019/5/24
 * Time: 12:27
 */

namespace app\kirito\controller;


use think\Controller;
use think\Db;
use think\facade\Request;
use think\facade\Session;

class Login extends Controller{
    //    ------------------------------------登录开始------------
    function login(){
        if(Request::isGet()){
            return view();
        }
        if(Request::isPost()){
            $login=$_REQUEST['login'];
            $password=$_REQUEST['password'];
            $a=db('User');
            $data = $a->getBylogin($login);
            if(empty($data)){
                return json(['status'=>false,'msg'=>'该账号不存在！']);
            }
            if($data['password']!=$password){
                return json(['status'=>false,'msg'=>'密码错误！']);
            }
            Session::set('id',$data['id']);
            return json(['status'=>true]);
        }
    }
    //    ------------------------------------登录结束------------

//    ------------------------------------主页开始------------
    function index(){
        if(Request::isGet()){
            if(Session::get('id')==null){
                echo $this->error('请先登录','Login/login');
            }else{
                $data=db('User')->find(Session::get('id'));
                $name=$data['name'];

                $id=$data['role_id'];
                $url=db('operation')->query(
                    "select operation.url from operation join role_operation on operation.id =  role_operation.operation_id where $id=role_operation.role_id"
                );
                return view('Login/index',[
                    'name'=>$name,
                    'url'=>$url
                ]);
            }
        }
    }
    //    ------------------------------------主页结束------------

//    ------------------------------------注册开始------------
    function registration(){
        if(Request::isGet()){
            return view();
        }
        if(Request::isPost()){
            $login=$_REQUEST['login'];
            $password=$_REQUEST['password'];
            $name=$_REQUEST['name'];
            $role_id=$_REQUEST['role_id'];
            $a=db('User');
            $data = $a->getBylogin($login);
            if(!empty($data)){
                return json(['status'=>false,'msg'=>'该账号已被注册！']);
            }else{
                $data1 = ['login' => $login, 'password' => $password,'name' =>  $name,'role_id' =>  $role_id];
                Db::name('user')
                    ->data($data1)
                    ->insert();
                return json(['status'=>true,'msg'=>'注册成功']);
            }
        }
    }
    //    ------------------------------------注册结束------------

//--------------------------------------------退出登录开始----------
    function out(){
        Session::delete('id');
        return redirect('Login/login');
    }
//--------------------------------------------退出登录结束----------

//----------------------------------------------welcome开始----------------------
    function welcome(){
        if(Request::isGet()){
            $a=db('User')->where('id=?',[Session::get('id')])->find();
            if($a['role_id']==2){
                $this->assign('a','销售管理');
                return $this->fetch();
            }
            if($a['role_id']==3){
                $this->assign('a','主管管理');
                return $this->fetch();
            }
            if($a['role_id']==4){
                $this->assign('a','总监管理');
                return $this->fetch();
            }
            if($a['role_id']==1){
                $this->assign('a','用户');
                return $this->fetch();
            }
        }
    }
//-----------------------------------------------welcome结束---------------------

//-----------------------------------------------业务信息开始-----------
    function yewu(){
        if(Request::isGet()){
            $data=db('Yewu')->select();
            $data1=Db::table('Success')->where('kehu_id',Session::get('id'))->column('yewu_id');
            return view('',['data'=>$data,'data1'=>$data1]);
        }
    }

    function yewu1(){
        if(Request::isGet()){
            $id=$_REQUEST['id'];
            $a=db('Tiaoru')
                ->query('select * from tiaoru join user on kehu_id =  user.id where user.id='.Session::get('id'));
            $xiaoshou_id=$a[0]['xiaoshou_id'];

                $data = ['yewu_id' => $id, 'xiaoshou_id' =>$xiaoshou_id ,'kehu_id'=>Session::get('id')];
                Db::name('Success')
                    ->data($data)
                    ->insert();
                return json(['status'=>true]);
        }
    }
//-----------------------------------------------业务信息结束-----------

//-----------------------------------------------我的业务开始----------
    function wodeyewu(){
        $a=db('Yewu')
            ->query('select yewu.chanpin from yewu join success on success.yewu_id =  yewu.id where success.kehu_id='.Session::get('id'));
        return view('',['a'=>$a]);
    }
//-----------------------------------------------我的业务结束----------

//--------------------------------------------------个人信息开始-----------
    function geren(){
        if(Request::isGet()){
            $data=db('User')->find(Session::get('id'));
            $name=$data['name'];
            $tel=$data['tel'];
            $email=$data['email'];
            return view('',['name'=>$name,'tel'=>$tel,'email'=>$email]);
        }
    }
//--------------------------------------------------个人信息结束-----------

//------------------------------------------------------编辑个人信息开始---------
    function bianjigeren(){
        if(Request::isGet()) {
            return view();
        }
        if(Request::isPost()) {
            $tel=$_REQUEST['tel'];
            $email=$_REQUEST['email'];
            $data=db('User')
                ->where('id',Session::get('id'))
                ->update(['tel' => $tel,'email'=>$email]);
        }
    }
//-------------------------------------------------------编辑个人信息结束--------

//------------------------------------------------------改密码开始--------------
        function gaimima(){
            if(Request::isGet()) {
                $data=db('User')->find(Session::get('id'));
                $name=$data['name'];
                return view('',['name'=>$name]);
            }
            if(Request::isPost()) {
                $password=$_REQUEST['password'];
                db('User')
                    ->where('id',Session::get('id'))
                    ->update(['password'=>$password]);
            }
        }
//------------------------------------------------------改密码结束--------------

//--------------------------------------------------公共客户池开始-----------
    function publickehu(){
        if(Request::isGet()) {
            $data=db('User')
                ->query('select * from user where role_id=1 and tel is not null');

            $data2=db('Tiaoru')->column('kehu_id');

            $name=db('Tiaoru')
                ->query('select tiaoru.xiaoshou_name from tiaoru join user on tiaoru.kehu_id =  user.id ');

            $names=$name[0]['xiaoshou_name'];
            return view('',['data'=>$data,'data2'=>$data2,'names'=>$names]);
        }
    }
//--------------------------------------------------公共客户池结束-----------

//--------------------------------------------------我的客户池开始-----------
    function mykehu1(){
        if(Request::isGet()) {
            $id=$_REQUEST['id'];
            $data=db('User')->find(Session::get('id'));
            $name=$data['name'];
            $data = ['xiaoshou_id' => Session::get('id'), 'xiaoshou_name' => $name,'kehu_id'=>$id];
            Db::name('Tiaoru')
                ->data($data)
                ->insert();
        }
    }

    function mykehu(){
        if(Request::isGet()) {
            $a=db('User')
                ->query('select * from user join tiaoru on user.id =  tiaoru.kehu_id where tiaoru.xiaoshou_id='.Session::get('id'));
//            dump($a);
            $b=db('Success')->column("kehu_id");
            return view('',['a'=>$a,'b'=>$b]);
        }
    }
//--------------------------------------------------我的客户池结束-----------

//---------------------------------------------------挑入记录开始--------
    function tiaoru(){
        if(Request::isGet()) {
            $a=db('User')
                ->query('select user.name as k,tiaoru.time from user join tiaoru on user.id =  tiaoru.kehu_id where tiaoru.xiaoshou_id='.Session::get('id'));
            return view('',['a'=>$a]);
        }
    }
//---------------------------------------------------挑入记录结束--------

//---------------------------------------------------挑出记录开始-------
    function tiaochu1(){
        if(Request::isGet()) {
            $id=$_REQUEST['id'];
            $data=Db::table('Tiaoru')->find($id);
            Db::table('Tiaoru')->where('kehu_id',$data['kehu_id'])->delete();
        }
    }

    function tiaochu(){
        return view();
    }
//---------------------------------------------------挑出记录结束-------

//--------------------------------------------成功客户开始------------
    function successkehu(){
        $a=db('Yewu')
                    ->query('select * from yewu join success on yewu.id =  success.yewu_id');
        $b=db('User')
                    ->query('select user.name from user join success on user.id =  success.kehu_id');
        return view('',['a'=>$a,'b'=>$b]);
    }
//--------------------------------------------成功客户结束-------------

//------------------------------------------主管的公共客户池开始-------
    function zhuguanpublickehu(){
        if(Request::isGet()) {
            $data=db('User')
                ->query('select * from user where role_id=1 and tel is not null');

            $data2=db('Tiaoru')->column('kehu_id');

            $name=db('Tiaoru')
                ->query('select tiaoru.xiaoshou_name from tiaoru join user on tiaoru.kehu_id =  user.id ');

            $names=$name[0]['xiaoshou_name'];
            return view('',['data'=>$data,'data2'=>$data2,'names'=>$names]);
        }
    }

    function zhuguanpublickehu1(){
        if(Request::isGet()){
            $id=$_REQUEST['id'];
            db('Tiaoru')->query('delete from tiaoru where kehu_id='.$id);
        }
    }

    function zhusuccess(){
        if(Request::isGet()){
            $a=db('Yewu')
                ->query('select * from yewu join success on yewu.id =  success.yewu_id');
            $b=db('User')
                ->query('select user.name from user join success on user.id =  success.kehu_id');
            $c=db('User')
                ->query("select user.name from user join success on user.id = success.xiaoshou_id");
            $this->assign('a',$a);
            $this->assign('b',$b);
            $this->assign('c',$c);
            return $this->fetch();
        }
    }
//------------------------------------------主管的公共客户池结束-------

//-----------------------------------------------授权角色列表开始-------------
        function shouquan(){
            if(Request::isGet()){
                $a=db('User')->query('select * from user where role_id=2');
                $b=db('User')->query('select * from user where role_id=3');
                return view('',['a'=>$a,'b'=>$b]);
            }
        }
//-----------------------------------------------授权角色列表结束-------------

//-----------------------------------------------编辑角色权利开始-------------
    function bianji(){
        if(Request::isGet()){
            $a=db('User')->query('select * from user where role_id=2');
            $b=db('User')->query('select * from user where role_id=3');
            return view('',['a'=>$a,'b'=>$b]);
        }
    }

    function bianji1(){
        if(Request::isGet()){
            $id=$_REQUEST['id'];
            $a=db('User')->query('select * from user where id='.$id);
            $name=$a[0]['name'];
            $role_id=$a[0]['role_id'];
            return view('',['name'=>$name,'role_id'=>$role_id,'id'=>$id]);
        }
    }

    function bianji2(){
        if(Request::isGet()){
            $role_id=$_REQUEST['role_id'];
            $id=$_REQUEST['id'];
            if(!empty($role_id[0])){
                db('User')
                    ->where('id',$id)
                    ->update(['role_id' => 2]);
            }
            if(!empty($role_id[1])){
                db('User')
                    ->where('id',$id)
                    ->update(['role_id' => 3]);
            }
        }
    }
//-----------------------------------------------编辑角色权利结束-------------

//--------------------------------------------------添加业务开始--------------

function newyewu(){
   if(Request::isGet()){
       return view();
   }
    if(Request::isPost()){
        $a=input('post.');
        $data = ['chanpin' => $a['chanpin'],'goumai'=>$a['goumai'],'peichang'=>$a['peichang'],'qixian'=>$a['qixian']];
        dump(Db::name('yewu')->insert($data));
    }
}
//--------------------------------------------------添加业务结束--------------

}